package com.demo.hotel_book.controller;

import com.demo.hotel_book.entity.User;
import com.demo.hotel_book.mapper.UserMapper;
import com.demo.hotel_book.provide.EmailProvide;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class SignController {
    @Autowired
    UserMapper userMapper;
    @Autowired
    EmailProvide emailProvide;

    @RequestMapping("/")
    public String get(HttpSession session,Model model) {
        model.addAttribute("param1","城市、酒店等关键字");
        model.addAttribute("timeIn","预计入住时间");
        model.addAttribute("timeOut","预计离店时间");
        if (session.getAttribute("param")!=null)
        {
            String param=(String)session.getAttribute("param");
            String timeIn=(String)session.getAttribute("timeIn");
            String timeOut=(String)session.getAttribute("timeOut");
            model.addAttribute("param1",param);
            model.addAttribute("timeIn",timeIn);
            model.addAttribute("timeOut",timeOut);
        }
        return "index";
    }

    @RequestMapping("/signup")
    public String signup() {
        return "register";
    }


    @RequestMapping("/random")
    @ResponseBody
    public String random(String email, Model model) {
        String mima = emailProvide.sendTo(email);
        model.addAttribute("checkcode", mima);
        return mima;
    }


    @RequestMapping("/login")
    @ResponseBody
    public String signup(User user, HttpSession session) {
        User result = userMapper.loginUser(user);
        if (result != null) {
            session.setAttribute("user", result);
            String url = String.valueOf(session.getAttribute("searchId"));
            if (session.getAttribute("searchId") != null) {
                session.setAttribute("searchId", null);
                return "/rooms?id=" + url;
            } else
                return "/";
        } else {

            return "error";
        }
    }
    @GetMapping("/logout")
    public String logout(HttpSession session){
        session.setAttribute("user",null);
        session.setAttribute("param",null);
        session.setAttribute("timeIn",null);
        session.setAttribute("timeOut",null);
        return "redirect:/";
    }

    @RequestMapping("/regist")
    @ResponseBody
    public int regist(User user) {
        return userMapper.insertUser(user);
    }

    @RequestMapping("/selUser")
    @ResponseBody
    public String findUser(User user) {
        User re = userMapper.seluerByname(user);
        if (re == null)
            return "true";
        else
            return "false";
    }

    @RequestMapping("/reset")
    @ResponseBody
    public int reset(User user) {
        return userMapper.reset(user);
    }


    @RequestMapping("/offer")
    public String goOffer() {
        return "offers";
    }
}
